local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablestatesvrhelper"
local base = require "base"
require "enum"

local TableStatesvrRequest = {}

function TableStatesvrRequest.process(session, source, event, ...)
	local f = TableStatesvrRequest[event] 
	if f == nil then
		return
	end
	f(...)
end

function TableStatesvrRequest.gettablestatebycreateid(request)
	local server = msghelper:get_server()	
	local table_pool = server.table_pool
	local create_table_indexs = server.create_table_indexs
	local createusers_table_indexs = server.createusers_table_indexs
	local createtablelist = createusers_table_indexs[request.rid]

	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid,
	}
	local game_createtable_indexs = create_table_indexs[request.gameid]
	if game_createtable_indexs == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_GAMEID
		responsemsg.errcodedes = "无效的游戏id！"
		base.skynet_retpack(responsemsg)
		return		
	end

	local id = game_createtable_indexs[request.create_table_id]
	local tableinfo = table_pool[id]

	--[[if tableinfo == nil and createtablelist ~= nil then
		for key, _ in pairs(createtablelist) do
			tableinfo = table_pool[key]
			if tableinfo ~= nil then
				responsemsg.tablestate = tableinfo
				base.skynet_retpack(responsemsg)
				return
			end
		end		
	end]]

	if id == nil 
		or tableinfo == nil 
		or tableinfo.state == ETableState.TABLE_STATE_UNKNOW then
		responsemsg.errcode = EErrCode.ERR_INVALID_CREATETABLEID
		responsemsg.errcodedes = "无效的桌号！"
		base.skynet_retpack(responsemsg)
		return
	end
	responsemsg.tablestate = tableinfo
	base.skynet_retpack(responsemsg)
end

function TableStatesvrRequest.getfriendtablelist(request)
	local server = msghelper:get_server()	
	local table_pool = server.table_pool
	local tableinfo
	local createusers_table_indexs = server.createusers_table_indexs

	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}

	local usertablelist = createusers_table_indexs[request.rid]	
	if usertablelist == nil then
		responsemsg.tablelist = {}
		base.skynet_retpack(responsemsg)
		return
	end

	responsemsg.tablelist = {}
	if usertablelist == nil then
		base.skynet_retpack(responsemsg)
		return
	end

	local dellist = {}	
	for id, _ in pairs(usertablelist) do
		tableinfo = table_pool[id]
		if tableinfo ~= nil 
			and tableinfo.state ~= ETableState.TABLE_STATE_UNKNOW then
			table.insert(responsemsg.tablelist, tableinfo)
		elseif tableinfo == nil then
			table.insert(dellist, id)
		end
	end
	base.skynet_retpack(responsemsg)

	for _, id in ipairs(dellist) do
		usertablelist[id] = nil
	end
end

function TableStatesvrRequest.getgamerooms(request)
	local server = msghelper:get_server()	
	local table_pool = server.table_pool
	local roomsvrs = server.roomsvrs
	local tableinfo = nil
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		tablestates = {},
	}
	--[[if request.room_type <= 0 then
		responsemsg.errcode = EErrCode.ERR_INVALID_ROOMTYPE
		responsemsg.errcodedes = "无效的场次类型！"
		base.skynet_retpack(responsemsg)
		return
	end
	local room_type = request.room_type
	for roomsvr_id, roomsvr in pairs(roomsvrs) do
		if roomsvr ~= nil and type(roomsvr) == "table" then
			local roomlist = roomsvr[room_type]
			if roomlist ~= nil then
				for gametype, gamelist in pairs(roomlist) do
					for id, _ in pairs(gamelist) do
						if id ~= "num" then
							if game_type_list[gametype] == nil then
								tableinfo = table_pool[id]
                                table.insert(responsemsg.tablestates, tableinfo)
                                tableinfo.totalplayernum = gamelist.num
                                game_type_list[gametype] = id
							else
								local tableid = game_type_list[gametype]
								local tableinfo = table_pool[tableid]
								local tatolnum = tableinfo.totalplayernum
								tableinfo.totalplayernum = tatolnum + gamelist.num
							end
							break
						end
					end
				end
			end
		end
	end]]

	base.skynet_retpack(responsemsg)
end

--添加快速开始逻辑
function TableStatesvrRequest.quickstart(request)
	local server = msghelper:get_server()
	local table_pool = server.table_pool
	local tableplayernumindexs = server.tableplayernumindexs

	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}
	--[[
	local room_list
	local game_list
	local table_list
	local tableinfo

	room_list = tableplayernumindexs[request.room_type]
	if room_list == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_ROOMTYPE
		responsemsg.errcodedes = "无效的场次类型！"
		base.skynet_retpack(responsemsg)
		return
	end
	game_list = room_list[request.game_type]
	if game_list == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_GAMETYPE
		responsemsg.errcodedes = "无效的游戏类型！"
		base.skynet_retpack(responsemsg)
		return
	end
	
	for i=3, 1, -1 do
		table_list = game_list[i]
		for id, _ in pairs(table_list) do
			tableinfo = table_pool[id]
			if tableinfo ~= nil and id ~= request.id and i < tableinfo.max_player_num and tableinfo.distribute_playernum < tableinfo.max_player_num then
				responsemsg.id = tableinfo.id
				responsemsg.roomsvr_id = tableinfo.roomsvr_id
				responsemsg.roomsvr_table_address = tableinfo.roomsvr_table_address 
				tableinfo.distribute_playernum = tableinfo.distribute_playernum + 1			    	
				base.skynet_retpack(responsemsg)
				return	
			end
		end
	end

	table_list = game_list[5]
	for id, _ in pairs(table_list) do
		tableinfo = table_pool[id]
		if tableinfo ~= nil and id ~= request.id then
			responsemsg.id = tableinfo.id
			responsemsg.roomsvr_id = tableinfo.roomsvr_id
			responsemsg.roomsvr_table_address = tableinfo.roomsvr_table_address 
			base.skynet_retpack(responsemsg)
			return
		end 
	end]]

	responsemsg.errcode = EErrCode.ERR_NO_VALID_TABLE
	responsemsg.errcodedes = "无可用的房间！"
	base.skynet_retpack(responsemsg)
end

--查询玩家某个游戏类型创建的房间号
function TableStatesvrRequest.search_createtable_bygameid(request)
	local server = msghelper:get_server()	
	local createusers_table_indexs = server.createusers_table_indexs
	local createtablelist = createusers_table_indexs[request.rid]
	local table_pool = server.table_pool

	if createtablelist == nil then
		base.skynet_retpack("")
		return
	end

	local tmptable
	local tmpdeletes = {}
	for id, _ in pairs(createtablelist) do		
		tmptable = table_pool[id]
		if tmptable ~= nil 
			and tmptable.gameid == request.gameid 
			and tmptable.state ~= 0
			and tmptable.state ~= ETableState.TABLE_STATE_GAME_START then
				base.skynet_retpack(tmptable.create_table_id)
				return
		elseif tmptable == nil then
			table.insert(tmpdeletes, id)
		end		
	end

	for _, id in ipairs(tmpdeletes) do
		createtablelist[id] = nil
	end

	base.skynet_retpack("")	
end

function TableStatesvrRequest.getfriendcointablelist(request)
	local server = msghelper:get_server()	
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
		gameid = request.gameid,
	}
	local tableplayernumindexs = server.tableplayernumindexs
	local table_pool = server.table_pool

	local room_list
	local table_list
	local tableinfo
	local count = 0

	local gametableplayernumindexs = tableplayernumindexs[request.gameid]


	room_list = gametableplayernumindexs[ERoomType.ROOM_TYPE_FRIEND_COIND]
	if room_list == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_ROOMTYPE
		responsemsg.errcodedes = "无效的场次类型"
		base.skynet_retpack(responsemsg)
		return
	end
	
	responsemsg.tablelist = {}
	--先取无密码的
	for i=9, 1, -1 do
		table_list = room_list[i]
		for id, _ in pairs(table_list) do
			tableinfo = table_pool[id]
			if tableinfo ~= nil
				and tableinfo.is_public 
				and i < tableinfo.max_player_num then
				table.insert(responsemsg.tablelist, tableinfo)
				count = count + 1
				if count >= request.max_showcoincreatetable_num then
					base.skynet_retpack(responsemsg)
					return
				end
			end
		end
	end

	--取无密码空房间
	table_list = room_list[10]
	for id, _ in pairs(table_list) do
		tableinfo = table_pool[id]
		if tableinfo ~= nil and tableinfo.is_public then
			table.insert(responsemsg.tablelist, tableinfo)
			count = count + 1
			if count >= request.max_showcoincreatetable_num then
				base.skynet_retpack(responsemsg)
				return
			end
		end
	end

	--取满房间
	for i=9, 1, -1 do
		table_list = room_list[i]
		for id, _ in pairs(table_list) do
			tableinfo = table_pool[id]
			if tableinfo ~= nil
				and tableinfo.is_public 
				and i >= tableinfo.max_player_num then
				table.insert(responsemsg.tablelist, tableinfo)
				count = count + 1
				if count >= request.max_showcoincreatetable_num then
					base.skynet_retpack(responsemsg)
					return
				end
			end
		end
	end

	base.skynet_retpack(responsemsg)
end

function TableStatesvrRequest.get_fndtable_num(request)
	local server = msghelper:get_server()	
	local createusers_table_indexs = server.createusers_table_indexs
	local createtablelist = createusers_table_indexs[request.rid]
	local table_pool = server.table_pool

	if createtablelist == nil then
		base.skynet_retpack(0)
		return
	end

	local num = 0
	local tmptable
	local dels = {}
	for id, _ in pairs(createtablelist) do
		tmptable = table_pool[id]
		if tmptable ~= nil then
			if tmptable.state ~= ETableState.TABLE_STATE_UNKNOW then
				num = num + 1
			end
		else
			table.insert(dels, id)
		end
	end

	for _, id  in ipairs(dels) do
		createtablelist[id] = nil
	end

	base.skynet_retpack(num)		
end

return TableStatesvrRequest